import api from './api';

export interface LogFile {
  name: string;
  path: string;
  size: number;
  modified: Date;
}

export interface LogContent {
  lines: string[];
}

export interface LogAnalysis {
  totalRequests: number;
  successRequests: number;
  errorRequests: number;
  avgResponseSize: number;
  totalResponseSize: number;
  statusCodes: Record<string, number>;
  topIPs: Record<string, number>;
  topURLs: Record<string, number>;
  topMethods: Record<string, number>;
  topUserAgents: Record<string, number>;
}

export class LogService {
  // 获取日志文件列表
  static async getLogFiles(): Promise<LogFile[]> {
    const response = await api.get('logs');
    return response.data.map((log: any) => ({
      ...log,
      modified: new Date(log.modified)
    }));
  }

  // 读取日志文件内容
  static async readLogFile(filePath: string, lines: number = 100, tail: boolean = false): Promise<LogContent> {
    const response = await api.get(`/logs/${encodeURIComponent(filePath)}`, {
      params: { lines, tail }
    });
    return response.data;
  }

  // 分析访问日志
  static async analyzeLog(filename: string, period: '1h' | '24h' | '7d' = '24h'): Promise<LogAnalysis> {
    const response = await api.get(`/logs/${encodeURIComponent(filename)}/analyze`, {
      params: { period }
    });
    return response.data;
  }
} 